﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;//引入命名空间
using System.Data;

public partial class gengxin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindYear();
            BindMonth();
            BindDay();
            BindProvince();
            ReadInfo();
        }
}

    protected void ReadInfo()//把用户的个人信息读取出来
    {
        string num = "";

        if (Request.QueryString["Knum"] != null)
        {
            num = Request.QueryString["Knum"].ToString().Trim();
        }

       // if (Session["tmpID"] != null)
       // {
       //    id = Session["tmpID"].ToString();
       // }
        txtNum.Text = num;

        kh tmp = new kh();
        tmp.num = num;

        SqlDataReader rd = tmp.Getonekh(tmp);

        while (rd.Read())
        {
            txtPw.Text = rd["Kpw"].ToString().Trim();
            txtName.Text = rd["Kname"].ToString().Trim();
            string birth = rd["Kbirth"].ToString().Trim();
            if (birth.Length != 0)
            {
                string year = birth.Substring(0, 4);
                int month = Convert.ToInt32(birth.Substring(4, 2));
                int day = Convert.ToInt32(birth.Substring(6, 2));
                drpYear.SelectedValue = year;
                drpMonth.SelectedValue = month.ToString();
                drpDay.SelectedValue = day.ToString();
            }

            string proid = rd["Kprovince"].ToString().Trim();
            if (proid.Length != 0)
            {
                drpProvince.SelectedValue = proid;
                BindCity(drpProvince.SelectedValue);
                string cityid = rd["Kcity"].ToString().Trim();
                if (cityid.Length != 0)
                {
                    drpCity.SelectedValue = cityid;
                }
            }

            txtQq.Text = rd["Kqq"].ToString().Trim();
            txtMail.Text = rd["Kmail"].ToString().Trim();
            txtPhone.Text = rd["Kphone"].ToString().Trim();
            txtAddress.Text = rd["Kaddress"].ToString().Trim();
            txtWork.Text = rd["Kwork"].ToString().Trim();
            txtIdcard.Text = rd["Kidcard"].ToString().Trim();
            string problem = rd["Kproblem"].ToString().Trim();
            if (problem.Length != 0)
            {
                drpProblem.SelectedValue = problem;
            }

            txtAnswer.Text = rd["Kanswer"].ToString().Trim();

        }
        rd.Close();
    }
    protected void drpProblem_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpProblem.SelectedIndex == 3)
        {
            txtProblem.Visible = true;
        }
        else
        {
            txtProblem.Visible = false;
        }
    }
    protected void BindYear()
    {
        drpYear.Items.Clear();
        int startYear = DateTime.Now.Year - 10;
        int currentYear = DateTime.Now.Year;
        for (int i = startYear; i <= currentYear; i++)
        {
            drpYear.Items.Add(new ListItem(i.ToString()));
        }
        drpYear.SelectedValue = currentYear.ToString();
    }
    protected void BindMonth()
    {
        drpMonth.Items.Clear();
        for (int i = 1; i <= 12; i++)
        {
            drpMonth.Items.Add(i.ToString());
        }
    }
    protected void BindDay()
    {
        drpDay.Items.Clear();
        string year = drpYear.SelectedValue;
        string month = drpMonth.SelectedValue;
        int days = DateTime.DaysInMonth(int.Parse(year), int.Parse(month));
        for (int i = 1; i <= days; i++)
        {
            drpDay.Items.Add(i.ToString());
        }
    }


    protected void BindProvince()
    {
        kh tmp = new kh();
        drpProvince.DataSource = tmp.BindProvince().Tables[0].DefaultView;
        drpProvince.DataTextField = "FName";
        drpProvince.DataValueField = "FID";
        drpProvince.DataBind();
    }

    protected void BindCity(string proid)
    {
        kh tmp = new kh();
        drpCity.DataSource = tmp.BindCity(proid).Tables[0].DefaultView;
        drpCity.DataValueField = "FcityID";
        drpCity.DataTextField = "FcityName";
        drpCity.DataBind();
    }

    protected void drpProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCity(drpProvince.SelectedValue.Trim());
    }

    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDay();
    }
    protected void drpMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDay();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string num = txtNum.Text.Trim();
        string name = txtName.Text.Trim();
        string pw = txtPw.Text.Trim();
        string year = drpYear.SelectedValue.Trim();
        string month = drpMonth.SelectedValue.Trim();
        string day = drpDay.SelectedValue.Trim();
        string bitrh = "";
        if (month.Length == 1)
        {
            month = "0" + month;
        }
        if (day.Length == 1)
        {
            day = "0" + day;
        }
        bitrh = year + month + day;


        string proid = drpProvince.SelectedValue.Trim();
        string cityid = drpCity.SelectedValue.Trim();
        string qq = txtQq.Text.Trim();
        string phone = txtPhone.Text.Trim();
        string mail = txtMail.Text.Trim();
        string address = txtAddress.Text.Trim();
        string problem = txtProblem.Text.Trim();
        string answer = txtAnswer.Text.Trim();
        string work = txtWork.Text.Trim();
        string idcard = txtIdcard.Text.Trim();

        kh tmp = new kh();
        tmp.num = num;
        tmp.name = name;
        tmp.pw = pw;
        tmp.birth = bitrh;
        tmp.province = proid;
        tmp.city = cityid;
        tmp.qq = qq;
        tmp.mail = mail;
        tmp.phone = phone;
        tmp.address = address;
        tmp.problem = problem;
        tmp.answer = answer;
        tmp.work = work;
        tmp.idcard = idcard;
        int result = tmp.Updateoneuser(tmp);
        if (result > 0)
        {
            Response.Write("<script> alert(\"更新个人资料成功！\")</script>");
        }

        else
        {
            Response.Write("<script> alert(\"更新个人资料失败！\")</script>");
        }
    }
    }

     









       
        


    
